import turtle
import time
import random
import sys
from turtle import textinput, done

# 设置中文字体支持
try:
    # 尝试设置中文字体
    font_config = ("SimHei", 16, "normal")
except:
    # 如果没有中文字体，使用默认字体
    font_config = ("Arial", 16, "normal")

# 摩斯密码映射 - "我爱你"的摩斯密码
morse_code = {
    '我': '···-·',
    '爱': '·-··',
    '你': '·--'
}

# 生成"我爱你"的摩斯序列
message = "我爱你"
morse_sequence = []
for char in message:
    morse_sequence.extend(list(morse_code[char]))
    morse_sequence.append(' ')  # 字符间空格
morse_sequence.pop()  # 移除最后一个空格

# 屏幕设置
screen = turtle.Screen()
screen.title("摩斯密码表白器")
screen.setup(width=800, height=600)
screen.bgcolor("#FFF0F3")  # 浅粉色背景

# 大爱心绘制函数
def draw_heart(size, color, opacity=1.0):
    turtle.penup()
    turtle.goto(0, -100)
    turtle.pendown()
    turtle.color(color)
    turtle.begin_fill()
    turtle.left(140)
    turtle.forward(size)
    turtle.circle(-size/2, 180)
    turtle.left(120)
    turtle.circle(-size/2, 180)
    turtle.forward(size)
    turtle.end_fill()
    turtle.setheading(0)  # 重置方向

# 小爱心类（用于弹幕）
class SmallHeart:
    def __init__(self):
        self.t = turtle.Turtle()
        self.t.hideturtle()
        self.t.speed(0)
        self.t.penup()
        self.size = random.randint(10, 30)
        self.x = random.randint(-350, 350)
        self.y = -300  # 从底部开始
        self.t.goto(self.x, self.y)
        self.speed = random.uniform(1, 3)
        self.angle = 0
        self.angle_speed = random.uniform(-2, 2)
        self.color = (random.uniform(0.8, 1), random.uniform(0.4, 0.6), random.uniform(0.5, 0.7))
        
    def move(self):
        self.y += self.speed
        self.angle += self.angle_speed
        self.x += random.uniform(-1, 1)  # 随机左右移动
        self.t.clear()
        self.t.penup()
        self.t.goto(self.x, self.y)
        self.t.setheading(self.angle)
        self.draw()
        
    def draw(self):
        self.t.color(self.color)
        self.t.begin_fill()
        self.t.left(140)
        self.t.forward(self.size)
        self.t.circle(-self.size/2, 180)
        self.t.left(120)
        self.t.circle(-self.size/2, 180)
        self.t.forward(self.size)
        self.t.end_fill()
        self.t.setheading(0)

# 显示文本
def show_text(message, y_pos, size=16):
    text_turtle = turtle.Turtle()
    text_turtle.hideturtle()
    text_turtle.speed(0)
    text_turtle.penup()
    text_turtle.goto(0, y_pos)
    text_turtle.color("#594A4E")
    try:
        text_turtle.write(message, align="center", font=("SimHei", size, "normal"))
    except:
        text_turtle.write(message, align="center", font=("Arial", size, "normal"))
    return text_turtle

# 主程序
def main():
    # 初始化大爱心绘制 turtle
    heart_turtle = turtle.Turtle()
    heart_turtle.hideturtle()
    heart_turtle.speed(0)
    
    # 显示提示信息
    prompt = show_text("这段闪烁的摩斯密码代表着一个秘密信息，你能破译它的含义吗？", 150)
    
    # 获取用户输入
    user_input = textinput("破译密码", "请输入你破译的内容:")
    
    # 检查答案
    if user_input == message:
        # 清除原有内容
        heart_turtle.clear()
        prompt.clear()
        
        # 显示成功信息
        success_text = show_text("恭喜你破译成功！我也爱你 ❤️", 150, 20)
        show_text("点击屏幕关闭窗口，重新运行程序可再次体验", 120, 12)
        
        # 绘制跳动的爱心
        def animate_heart():
            for i in range(100):
                heart_turtle.clear()
                scale = 1 + 0.1 * abs(turtle.math.sin(i/10))
                heart_turtle.penup()
                heart_turtle.goto(0, -100)
                heart_turtle.pendown()
                heart_turtle.color("#FF85A2")
                heart_turtle.begin_fill()
                heart_turtle.left(140)
                heart_turtle.forward(100 * scale)
                heart_turtle.circle(-50 * scale, 180)
                heart_turtle.left(120)
                heart_turtle.circle(-50 * scale, 180)
                heart_turtle.forward(100 * scale)
                heart_turtle.end_fill()
                heart_turtle.setheading(0)
                screen.update()
                time.sleep(0.05)
            animate_heart()
        
        # 创建爱心弹幕
        hearts = [SmallHeart() for _ in range(30)]
        
        def animate_hearts():
            for heart in hearts:
                heart.move()
                # 当爱心超出屏幕顶部，重置位置
                if heart.y > 300:
                    heart.y = -300
                    heart.x = random.randint(-350, 350)
            screen.update()
            screen.ontimer(animate_hearts, 50)
        
        # 开始动画
        animate_heart()
        animate_hearts()
        
    else:
        # 答案错误
        heart_turtle.clear()
        prompt.clear()
        show_text("破译失败，请再试一次！", 0, 20)
        show_text("程序将在3秒后关闭", -30, 16)
        time.sleep(3)
        screen.bye()
        return
    
    # 点击关闭
    screen.exitonclick()

# 摩斯密码闪烁动画
def morse_animation():
    index = 0
    while True:
        heart_turtle.clear()
        symbol = morse_sequence[index]
        
        if symbol == '·':  # 点
            draw_heart(100, "#FF85A2")
        elif symbol == '-':  # 划
            draw_heart(100, "#FF85A2")
        else:  # 空格
            draw_heart(100, "#FF85A2", 0.3)  # 较暗
        
        screen.update()
        index = (index + 1) % len(morse_sequence)
        time.sleep(0.7)  # 符号间隔

# 初始化大爱心turtle
heart_turtle = turtle.Turtle()
heart_turtle.hideturtle()
heart_turtle.speed(0)

# 启动程序
if __name__ == "__main__":
    # 启动摩斯密码动画
    import threading
    animation_thread = threading.Thread(target=morse_animation, daemon=True)
    animation_thread.start()
    
    # 启动主程序
    main()
    
    done()
    sys.exit()
